package com.sg.file.transform.provider.center;

import com.sg.file.transform.api.dto.TransformReqeust;
import it.sauronsoftware.jave.EncoderException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

/**
 * Created by asus on 2018/7/20.
 */
@Slf4j
public class TransformCenter {
    public static boolean doTransform(TransformReqeust transformReqeust){
        boolean flag = false;
         try {
             //获取转化类
             TransformCode transformCode = null;
             try {
                 transformCode = (TransformCode) Class.forName(transformReqeust.getTransformClassName()).newInstance();
             } catch (ClassNotFoundException e) {
                 log.error("获取转码类失败：未找到类");
             } catch (IllegalAccessException e) {
                 log.error("获取转码类失败：实例化失败");
             } catch (InstantiationException e) {
                 log.error("获取转码类失败：实例化失败");
             }
             //如果转码类为空直接失败
            if(transformCode==null){
                 log.error("获取转码类失败：类为空");
                 return false;
            }
            //执行转换
            transformCode.doTransform(transformReqeust);
            //创建转换后文件
            File file = new File(transformReqeust.getTransformFilePath());
            //循环判断文件是否已存在,如果不存在则说明文件转换未完成，则继续循环判断。
            while(!file.exists()){
            }
            flag = true;
        }catch (EncoderException e) {
            log.error("格式化失败：{}",e.getMessage());
        } catch (IOException e) {
            log.error("文件创建：{}",e.getMessage());
        }finally{
            return flag;
        }
    }
}
